package de.tabuma.wsdl2xform.xform.view;

import de.tabuma.wsdl2xform.xform.entity.Project;
import de.tabuma.lib.tabumaview;
import de.tabuma.wsdl2xform.view.*;
import de.tabuma.wsdl2xform.xform.*;
import java.util.*;
import java.util.logging.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.3 $
 * @since $Date: 2009/12/30 01:10:56 $
 */
public class projectTable extends tabumaview {

    private static Logger log = Logger.getLogger(projectTable.class.getName());

    public projectTable(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }
    
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {

            log.fine("processRequest");

            String url = de.tabuma.wsdl2xform.controller.constants.SERVLET_NAME + "?";
            url += de.tabuma.wsdl2xform.controller.constants.MAINNAV + "=" + de.tabuma.wsdl2xform.controller.constants.NAVID_CREATEXFORM;
            url += "&amp;";
            url += constants.XFORM_NAV + "=" + constants.NAV_SHOW_PROJECT;
            url += "&amp;";
            url += constants.PROJECT_ID + "=";

            out.println("<div class=\"contentContent\">");
            out.println("\t<table class=\"table\">");
            out.println("\t\t<tr>");
            out.println("\t\t\t<td class=\"tableheader\">"+constants.getValue("ID")+"</td>");
            out.println("\t\t\t<td class=\"tableheader\">"+constants.getValue("projectname")+"</td>");
            out.println("\t\t\t<td class=\"tableheader\">"+constants.getValue("mode")+"</td>");
            out.println("\t\t\t<td class=\"tableheader\">"+constants.getValue("usedcreationlicenses")+"</td>");
            out.println("\t\t\t<td class=\"tableheader\">"+constants.getValue("usedcalllicenses")+"</td>");
            out.println("\t\t\t<td class=\"tableheader\"></td>");
            out.println("\t\t</tr>");

            List<Project> projectList = (List<Project>) request.getAttribute(constants.PROJECT_LIST);

            log.fine("projectList:"+projectList);
            log.fine("start table");

            int countCreationLicense = 0;
            int countCallLicense = 0;

            if(projectList!=null) {
                Iterator<Project> projectIterator = projectList.iterator();
                while(projectIterator.hasNext()) {
                    Project project = projectIterator.next();
                    log.fine("project:"+project);
                    out.println("\t\t<tr>");
                    log.fine("projectid:"+project.getId());
                    out.println("\t\t\t<td class=\"tablecontent\">"+project.getId()+"</td>");
                    log.fine("projectid:"+project.getName());
                    out.println("\t\t\t<td class=\"tablecontent\"><a href=\""+url+project.getId()+"\">"+project.getName()+"</a></td>");

                    if(project.getExtendedMode()==true) {
                        out.println("\t\t\t<td class=\"tablecontent\">"+"extended Mode"+"</td>");
                    }
                    else {
                        out.println("\t\t\t<td class=\"tablecontent\">"+"simple Mode"+"</td>");
                    }

                    out.println("\t\t\t<td class=\"tablecontent\">"+"</td>");
                    out.println("\t\t\t<td class=\"tablecontent\">"+"</td>");
                    out.println("\t\t\t<td class=\"tablecontent\">"+"drop"+"</td>");
                    out.println("\t\t</tr>");
                }
            }

            log.fine("end table");

            out.println("\t\t<tr>");
            out.println("\t\t\t<td class=\"tablefooter\" colspan=\"3\">"+constants.getValue("usedlicenses")+"</td>");
            out.println("\t\t\t<td class=\"tablefooter\">"+countCreationLicense+"</td>");
            out.println("\t\t\t<td class=\"tablefooter\">"+countCallLicense+"</td>");
            out.println("\t\t</tr>");
            out.println("\t</table>");

            if(request.getAttribute(de.tabuma.wsdl2xform.controller.constants.ADMIN_NO_START_BUTTON)==null) {
                out.println("\t<form action=\""+de.tabuma.wsdl2xform.controller.constants.SERVLET_NAME+"\">");
                out.println("\t\t<input type=\"hidden\" name=\""+de.tabuma.wsdl2xform.controller.constants.MAINNAV+"\" value=\""+de.tabuma.wsdl2xform.controller.constants.NAVID_CREATEXFORM+"\"/>");
                out.println("\t\t<input type=\"hidden\" name=\""+constants.XFORM_NAV+"\" value=\""+constants.NAV_FIRST_STEP+"\"/>");
                out.println("\t\t<input type=\"submit\" value=\"create project\" class=\"loginbutton\"/>");
                out.println("\t</form>");
            }
            out.println("</div>");

        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
    }

}
